import React, { useEffect } from "react";
import { useNavigate, useLocation, Outlet } from "react-router-dom";

interface CompProps {
	to: string;
	from?: string;
}

export default function Redirect({ to, from }: CompProps) {
	const navigate = useNavigate();
	const location = useLocation();

	useEffect(() => {
		if (from && location.pathname !== from) return;

		navigate(to);
	}, [navigate, location, from, to]);

	return <Outlet />;
}
